package com.lanou.wuyou.gift.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.base.BaseFragment;
import com.lanou.wuyou.gift.bean.HomeFirstBean;
import com.lanou.wuyou.gift.bean.HomeFirstHeadBean;
import com.lanou.wuyou.gift.bean.HomeFirstTitleViewBean;
import com.lanou.wuyou.gift.tools.NetResultInterface;
import com.lanou.wuyou.gift.tools.NetWorkInstance;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/16.
 */

public class HomeFirstFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    private ListView listView;
    private ViewPager viewPager;
    private int currentPosition;
    private LinearLayout pointL;
    private Handler handler;
    private Runnable runnable;
    private HomeFirstAdapter adapter;
    private String url = "http://api.liwushuo.com/v2/secondary_banners?gender=1&generation=2";
    private HomeFirstBean homeFirstBean;
    private HomeFirstTitleViewBean homeFirstTitleViewBean;
    private String url1 = "http://api.liwushuo.com/v2/banners?channel=IOS";
    private RefreshLayout refreshLayout;

    public static HomeFirstFragment newInstance(String url){
        Bundle args = new Bundle();
        args.putString("url",url);
        HomeFirstFragment homeFirstFragment = new HomeFirstFragment();
        homeFirstFragment.setArguments(args);
        return homeFirstFragment;
    }

    @Override
    public int setLayout() {
        return R.layout.fragment_first_home;
    }

    @Override
    public void initView(View view) {
        //精选页listView
        listView = (ListView) view.findViewById(R.id.list_view_first_home);
        refreshLayout = (RefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
    }

    @Override
    public void initData() {
        View view = LayoutInflater.from(context).inflate(R.layout.head_home_first,null);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager_home_first_top);
        pointL = (LinearLayout) view.findViewById(R.id.point_home_first);
        homeFirstTitleViewBean = new HomeFirstTitleViewBean();
        refreshLayout.setOnRefreshListener(this);

        ImageView view1 = (ImageView) view.findViewById(R.id.home_first_head_view1);
        view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),HomeHeadSecondWebView.class);
                startActivity(intent);
            }
        });
        ImageView view2 = (ImageView) view.findViewById(R.id.home_first_head_view2);
        view2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),HomeHeadSecondWebViewTwo.class);
                startActivity(intent);
            }
        });
        ImageView view3 = (ImageView) view.findViewById(R.id.home_first_head_view3);
        view3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),HomeHeadSecondWebViewThree.class);
                startActivity(intent);
            }
        });
        ImageView view4 = (ImageView) view.findViewById(R.id.home_first_head_view4);
        view4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),HomeHeadSecondWebViewFour.class);
                startActivity(intent);
            }
        });
        ImageView view5 = (ImageView) view.findViewById(R.id.home_first_head_view5);
        view5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),HomeHeadSecondWebViewFive.class);
                startActivity(intent);
            }
        });
        ImageView view6 = (ImageView) view.findViewById(R.id.home_first_head_view6);
        view6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),HomeHeadSecondWebViewSix.class);
                startActivity(intent);
            }
        });

        NetWorkInstance.getInstance().netRequest(url1, new NetResultInterface() {
            @Override
            public void success(String str) {
                Gson gson = new Gson();
                homeFirstTitleViewBean = gson.fromJson(str,HomeFirstTitleViewBean.class);
                HomeFirstTopViewAdapter adapter = new HomeFirstTopViewAdapter(context);
                adapter.setBean(homeFirstTitleViewBean);
                viewPager.setAdapter(adapter);
                viewPager.setCurrentItem(1,false);

                if (homeFirstTitleViewBean != null){
                    if (homeFirstTitleViewBean.getData() != null){
                        //循环创建显示圆点的imageView，注意循环次数
                        for (int i = 0; i < homeFirstTitleViewBean.getData().getBanners().size(); i++) {
                            //创建控件
                            ImageView pointIv = new ImageView(getContext());
                            //设置间距
                            pointIv.setPadding(1, 1, 1, 1);
                            //设置尺寸（坐标）等信息
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);
                            pointIv.setLayoutParams(params);
                            //设置图片（第一个圆点显示白色，其余显示灰色）
                            if (i == 0) {
                                pointIv.setImageResource(R.mipmap.point_white);
                            } else {
                                pointIv.setImageResource(R.mipmap.point_grey);
                            }
                            //将图片添加到线性布局中
                            pointL.addView(pointIv);
                        }
                    }
                }
            }

            @Override
            public void fail(String str) {

            }
        });
        listView.addHeaderView(view);

        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                handler.postDelayed(runnable,2000);
                int currentPage = viewPager.getCurrentItem();
                currentPage++;
                viewPager.setCurrentItem(currentPage);
            }
        };
        handler.postDelayed(runnable,2000);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                currentPosition = position;

                for (int i = 0; i < homeFirstTitleViewBean.getData().getBanners().size() ; i++) {
                    ImageView img = (ImageView) pointL.getChildAt(i);
                    img.setImageResource(R.mipmap.point_grey);
                }
            }

            @Override
            public void onPageSelected(int position) {

                    handler.removeCallbacks(runnable);
                    handler.postDelayed(runnable,2000);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

                int currentPage = currentPosition;
                if (state == 0){
                    if (homeFirstTitleViewBean != null){
                    if (currentPosition == 0) {
                        viewPager.setCurrentItem(viewPager.getAdapter().getCount() - 2, false);
                        currentPage = viewPager.getAdapter().getCount() - 2 ;
                    }
                        if (homeFirstTitleViewBean.getData() != null){

                    if (currentPosition == viewPager.getAdapter().getCount() - 1){
                        viewPager.setCurrentItem(1,false);
                        currentPage = 1;
                    }
                        }
                    ImageView imageView = (ImageView) pointL.getChildAt(currentPage - 1);
                    imageView.setImageResource(R.mipmap.point_white);
                    }
                }
            }
        });

        adapter = new HomeFirstAdapter(context);
        listView.setAdapter(adapter);
        String urlStr = getArguments().getString("url");
        NetWorkInstance.getInstance().netRequest(urlStr, new NetResultInterface() {

            @Override
            public void success(String str) {
                Gson gson = new Gson();
                homeFirstBean = gson.fromJson(str,HomeFirstBean.class);
                adapter.setHomeFirstBean(homeFirstBean);
                refreshLayout.setRefreshing(false);
            }

            @Override
            public void fail(String str) {

            }
        });

        final ImageView imageView1 = (ImageView) view.findViewById(R.id.home_first_head_view1);
        final ImageView imageView2 = (ImageView) view.findViewById(R.id.home_first_head_view2);
        final ImageView imageView3 = (ImageView) view.findViewById(R.id.home_first_head_view3);
        final ImageView imageView4 = (ImageView) view.findViewById(R.id.home_first_head_view4);
        final ImageView imageView5 = (ImageView) view.findViewById(R.id.home_first_head_view5);
        final ImageView imageView6 = (ImageView) view.findViewById(R.id.home_first_head_view6);
        NetWorkInstance.getInstance().netRequest(url, new NetResultInterface() {
            @Override
            public void success(String str) {
                Gson gson = new Gson();
                HomeFirstHeadBean bean = gson.fromJson(str,HomeFirstHeadBean.class);
                Picasso.with(context).load(bean.getData().getSecondary_banners().get(0).getImage_url()).into(imageView1);
                Picasso.with(context).load(bean.getData().getSecondary_banners().get(1).getImage_url()).into(imageView2);
                Picasso.with(context).load(bean.getData().getSecondary_banners().get(2).getImage_url()).into(imageView3);
                Picasso.with(context).load(bean.getData().getSecondary_banners().get(3).getImage_url()).into(imageView4);
                Picasso.with(context).load(bean.getData().getSecondary_banners().get(4).getImage_url()).into(imageView5);
                Picasso.with(context).load(bean.getData().getSecondary_banners().get(5).getImage_url()).into(imageView6);
            }

            @Override
            public void fail(String str) {

            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int mID = homeFirstBean.getData().getItems().get(position - 1).getId();
                Intent intent = new Intent(getActivity(),HomeItemWebView.class);
                intent.putExtra("id",mID);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onRefresh() {
        String urlStr = getArguments().getString("url");
        NetWorkInstance.getInstance().netRequest(urlStr, new NetResultInterface() {

            @Override
            public void success(String str) {
                Gson gson = new Gson();
                homeFirstBean = gson.fromJson(str,HomeFirstBean.class);
                adapter.setHomeFirstBean(homeFirstBean);
                refreshLayout.setRefreshing(false);
            }

            @Override
            public void fail(String str) {

            }
        });
    }
}
